package com.lailang.lailang;

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ImageView;

import java.util.ArrayList;

/**
 * Created by david on 17-2-6.
 */
public class FirstFragment extends Fragment {

    private LLScrollView scrollView;

    private View navbarBG;
    private Button locationBtn;
    private Button searchBtn;

    private ViewPager imgSlider;
    private ArrayList<ImageView> sliderImgViews;
    private LayoutInflater mInflater;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mInflater = inflater;
        View rootView = inflater.inflate(R.layout.fragment_first, container, false);

        Typeface iconfont = Typeface.createFromAsset(getActivity().getAssets(), "iconfont.ttf");
        locationBtn = (Button) rootView.findViewById(R.id.locate_btn);
        locationBtn.setTypeface(iconfont);

        navbarBG = rootView.findViewById(R.id.navbar_first_bg);

        final AlphaAnimation animation = new AlphaAnimation(0.5f, 1.0f);
        final AlphaAnimation animation1 = new AlphaAnimation(1.0f, 0.5f);

        animation.setDuration(300);
        animation1.setDuration(300);

        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                searchBtn.setAlpha(1.0f);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        animation1.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                searchBtn.setAlpha(0.5f);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        searchBtn = (Button) rootView.findViewById(R.id.search_btn);
        searchBtn.setTypeface(iconfont);

        scrollView = (LLScrollView) rootView.findViewById(R.id.first_scrollview);
        scrollView.setScrollViewListener(new LLScrollViewListener() {
            @Override
            public void onScrollViewChanged(LLScrollView llScrollView, int x, int y, int oldX, int oldY) {
//                Log.d("scroll", String.valueOf(x)+", "+String.valueOf(y)+", "+String.valueOf(oldX)+", "+String.valueOf(oldY));
                if (y <= 0) navbarBG.setAlpha(0);

                if (y > 0 && y < 48) navbarBG.setAlpha((float)y / 64.0f);

                if (y >= 48) navbarBG.setAlpha(1);

                if (oldY >= 48 && y <= 48) searchBtn.startAnimation(animation1);
                if (oldY <= 48 && y >= 48) searchBtn.startAnimation(animation);

            }
        });

        imgSlider = (ViewPager) rootView.findViewById(R.id.slider_first);

        initImgSlider();

        Button kbBtn = (Button) rootView.findViewById(R.id.kb_btn);
        kbBtn.setTypeface(iconfont);
        SpannableStringBuilder spanStr = new SpannableStringBuilder(kbBtn.getText());
        spanStr.setSpan(new AbsoluteSizeSpan(64),0,2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanStr.setSpan(new AbsoluteSizeSpan(9),2,kbBtn.getText().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        kbBtn.setText(spanStr);

        Button ebBtn = (Button) rootView.findViewById(R.id.eb_btn);
        ebBtn.setTypeface(iconfont);
        ebBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("first", "eb_click");
            }
        });

        return rootView;
    }

    private void initImgSlider(){
        sliderImgViews = new ArrayList<>();

        //TODO: 先获取本地缓存的图片
        //如果没有，则显示资源图片
        ImageView iv1 = (ImageView) mInflater.inflate(R.layout.slider_imgview, null);
        iv1.setImageResource(R.mipmap.swiperone);
        sliderImgViews.add(iv1);

        ImageView iv2 = (ImageView) mInflater.inflate(R.layout.slider_imgview, null);
        iv2.setImageResource(R.mipmap.swipertwo);
        sliderImgViews.add(iv2);

        imgSlider.setAdapter(new ImgSliderAdapter());
        imgSlider.setCurrentItem(0);
    }


    private class ImgSliderAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return sliderImgViews.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
//            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView iv = sliderImgViews.get(position);
//            iv.setImageResource(R.mipmap.swiperone);
            container.addView(iv);
            return iv;
        }
    }
}
